<?php
namespace App\Jobs;
use App\Models\SmsConfig;
use App\Models\XnBatchOrder;
use App\Models\XnBillDetails;
use App\Models\XnMyyGoodsInterconnection;
use App\Services\SmsService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Throwable;

class OrderCallbackNotify implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     */
    public function __construct(
        public XnBatchOrder $order,
        public bool $result,
        public string $errMessage = '充值失败',
        public string $cami = '')
    {}

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        try {
            DB::transaction(function () {
                    $this->result ? $this->success() : $this->error();
            });
            if($this -> result){
                $smsInfo = SmsConfig::query() -> where('state',4) -> where('type',1) -> first();
                $success = SmsService::send($this->order->userName, [],$smsInfo->signName,$smsInfo->templateCode,1);
                if ($success) {
                    Log::channel('SendSms')->error('异步发送短信发送成功', [
                        'order'     => $this->order->toArray(),
                        'result'    => $this->result
                    ]);
                }
            }
        } catch (Throwable $e) {
            Log::channel('job')->error('回调异常:' . $e->getMessage(), [
                'order'     => $this->order->toArray(),
                'result'    => $this->result,
                'errMessage'=> $this->errMessage,
            ]);
        }
    }

    /**
     * @return void
     */
    protected function success(): void
    {
        $this->order->update(['recharge_status' => 1, 'update_time' => date('Y-m-d H:i:s')]);
        XnBillDetails::query()->where('order_num', $this->order['orderNum'])->update(['recharge_status' => 1]);
    }

    /**
     * @return void
     */
    protected function error(): void
    {
        $supplierBills = XnBillDetails::query()->where('order_num', $this->order['orderNum'])->first();
        $supplierBillInfo = [
            'goods_explain' => $supplierBills['goods_explain'],
            'bookkeeping_type' => 2,
            'order_sn' => $supplierBills['order_num'],
            'account' => $supplierBills['account'],
            'price' => $supplierBills['price'],
            'goods_num' => $supplierBills['goods_num'],
            'business_type' => $supplierBills['business_type'],
            'budget_money' => $supplierBills['budget_money'],
            'recharge_status' => 4,
            'Supplier_status' => $supplierBills['Supplier_status'],
            'refund_time' => date('Y-m-d H:i:s'),
            'ctime' => $supplierBills['ctime'],
            'Merchant_name' => $supplierBills['merchant_name'],
            'goods_id' => $supplierBills['goods_id'],
            'brand_id' => $supplierBills['brand_id'],
        ];
        $this->order->update(['recharge_status' => 2, 'msg' => $this->errMessage,'update_time'=>date('Y-m-d H:i:s')]);
        $supplierBills->update(['recharge_status' => 2]);
        XnBillDetails::query()->insert($supplierBillInfo);

    }
}

